<?php
class LoginMatchController extends \Yaf\Controller_Abstract
{
    public function init(){
        Yaf\Dispatcher::getInstance()->disableView();
    }
    public function checkedAction()
    {
        $name = $_POST['username'];
        $pass = $_POST['password'];
//        $user = UsersModel::where('username','=',$name,'and','password','=',$pass)->get();
        $salt = UsersModel::where('username','=',$name)->select('salt')->get();
        $id = UsersModel::where('username','=',$name)->select('Id')->get();
        $status = UsersModel::where('username','=',$name)->select('status')->get();

        if (count($salt)>0){
            $result = json_decode($salt,true);
            $resalt = $result[0]['salt'];
            $passSalt = md5(''.$pass.$resalt);
//            获取用户id
            $rId = json_decode($id,true);
            $uid = $rId[0]['Id'];
//            获取用户名
            $user = UsersModel::where('password','=',$passSalt)->select('username')->get();
            $reuser = json_decode($user,true);
            $username = $reuser[0]['username'];
//            获取密码
            $passData = UsersModel::where('password','=',$passSalt)->select('password')->get();
            $repass = json_decode($passData,true);
            $password = $repass[0]['password'];

            if (count($status)>0){
//            取出数组中的值
                $result = json_decode($status,true);
                $statusCheck = $result[0]['status'];
                if ($statusCheck=='0'){
                    $msg = "账户未激活，请前往邮箱进行激活";
                    $flag=false;
                }else{
                    if (strcmp($username,$name)==0){
                        if (strcmp($password,$passSalt)==0){
//                      开启session
                        session_start();
//                        获取用户的权限
                        $type = UsersModel::where('username','=',$name)->select('type')->get();
                        if (count($type)>0){
//                            获取激活状态
                            $rtype = json_decode($type,true);
                            $retype = $rtype[0]['type'];

//                            //将用户保存在session中
                            $_SESSION['uid']=$uid;
                            $_SESSION['logname']=$name;
                            $_SESSION['type']=$retype;


                        }

                        $msg="登录成功";
                        $flag=true;
                        }
                    }else{
                        $msg = "账户或密码错误，请重新输入";
                        $flag=false;
                    }
                }
            }

        }else{
            $msg = "账户或密码错误，请重新输入";
            $flag=false;
        }
        $arr=array("msg"=>$msg,"flag"=>$flag);
        echo json_encode($arr);
    }


}
